package com.zg.autoflow.web;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.autoflow.req.QZWorkflowInstanceReq;
import com.zg.autoflow.req.WorkflowInstanceReq;
import com.zg.autoflow.req.WorkflowStepDataReq;
import com.zg.autoflow.req.processListReq;
import com.zg.autoflow.service.SettleWorkflowInstanceService;
import com.zg.autoflow.vo.*;
import com.zg.common.core.R;
import com.zg.common.core.dao.BaseEntity;
import com.zg.common.core.dao.auth.AuthUser;
import com.zg.common.core.dao.auth.AuthUserDao;
import com.zg.common.core.dao.autoflow.dao.SettleWorkflowInstanceDao;
import com.zg.common.core.dao.autoflow.dao.SettleWorkflowInstanceLinkDao;
import com.zg.common.core.dao.autoflow.dao.SettleWorkflowStepInstanceDao;
import com.zg.common.core.dao.autoflow.entity.SettleWorkflowInstance;
import com.zg.common.core.dao.autoflow.entity.SettleWorkflowInstanceLink;
import com.zg.common.core.dao.autoflow.entity.SettleWorkflowStepInstance;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author zg
*/
@Api(tags = {"流程实例"})
@RestController
@RequiredArgsConstructor
public class SettleWorkflowInstanceController {
	private final SettleWorkflowInstanceDao settleWorkflowInstanceDao;
	private final SettleWorkflowInstanceService settleWorkflowInstanceService;
	private final SettleWorkflowInstanceLinkDao settleWorkflowInstanceLinkDao;
	private final SettleWorkflowStepInstanceDao settleWorkflowStepInstanceDao;
	private final AuthUserDao authUserDao;

	@ApiOperation("查询流程实例信息")
	@PostMapping("/flow/settleWorkflowInstance/getWorkflowInstanceByInstCode")
	public R<SettleWorkflowInstanceVo> getWorkflowInstanceByInstCode(@RequestBody String instCode) {
		SettleWorkflowInstance obj = settleWorkflowInstanceDao.getOne(new LambdaQueryWrapper<SettleWorkflowInstance>().eq(SettleWorkflowInstance::getInstCode, instCode));
		SettleWorkflowInstanceVo settleWorkflowInstanceVo = new SettleWorkflowInstanceVo();
		BeanUtil.copyProperties(obj, settleWorkflowInstanceVo);
		return R.success(settleWorkflowInstanceVo);
	}

	@ApiOperation("查询待办列表")
	@PostMapping("/flow/settleWorkflowInstance/getTodoWorkflowInstancePage")
	public R<IPage<WorkflowInstanceVo>> getTodoWorkflowInstancePage(@RequestBody @Valid WorkflowInstanceReq workflowInstanceReq) {
		IPage<WorkflowInstanceVo> pb = settleWorkflowInstanceService.getTodoWorkflowInstancePage(new Page(workflowInstanceReq.getPage(), workflowInstanceReq.getPageSize()),workflowInstanceReq);
		return R.success(pb);
	}

	@ApiOperation("根据当前流程实例查询前置流程信息")
	@PostMapping("/flow/settleWorkflowInstance/getQZInstCode")
	public R<List<QZWorkflowVo>> getQZInstCode(@RequestBody QZWorkflowInstanceReq qzWorkflowInstanceReq) {
		List<QZWorkflowVo> a =new ArrayList();
		List<SettleWorkflowInstanceLink> obj = settleWorkflowInstanceLinkDao.list(new LambdaQueryWrapper<SettleWorkflowInstanceLink>().eq(SettleWorkflowInstanceLink::getToInstCode, qzWorkflowInstanceReq.getInstCode()));
		if ("0".equals(qzWorkflowInstanceReq.getType())) {
			if (ObjectUtils.isNotEmpty(obj)) {
				obj.forEach(line -> {
					QZWorkflowVo qzWorkflowVo = new QZWorkflowVo();
					SettleWorkflowInstance settleWorkflowInstance = settleWorkflowInstanceDao.getOne(new LambdaQueryWrapper<SettleWorkflowInstance>().eq(SettleWorkflowInstance::getInstCode, line.getFromInstCode()));
					qzWorkflowVo.setInstCode(settleWorkflowInstance.getInstCode());
					qzWorkflowVo.setFlowCode(settleWorkflowInstance.getFlowCode());
					qzWorkflowVo.setFlowName(settleWorkflowInstance.getFlowName());
					List<SettleWorkflowStepInstVo> settleWorkflowStepVos = new ArrayList<>();
					List<SettleWorkflowStepInstance> settleWorkflowStepInstance = settleWorkflowStepInstanceDao.list(new LambdaQueryWrapper<SettleWorkflowStepInstance>().eq(SettleWorkflowStepInstance::getInstCode, line.getFromInstCode()).eq(SettleWorkflowStepInstance::getStepInstState, '2').orderByAsc(SettleWorkflowStepInstance::getCreateDate));
					if (CollectionUtil.isNotEmpty(settleWorkflowStepInstance)){
						List<String> userId = settleWorkflowStepInstance.stream().map(SettleWorkflowStepInstance::getOperUser).distinct().collect(Collectors.toList());
						List<AuthUser> authUsers = authUserDao.listByIds(userId);
						Map<String,String> users = authUsers.stream().collect(Collectors.toMap(user -> { return user.getId() + ""; }, AuthUser::getName));
						for (SettleWorkflowStepInstance stepInstance : settleWorkflowStepInstance) {
							SettleWorkflowStepInstVo settleWorkflowStepVo = new SettleWorkflowStepInstVo();
							BeanUtil.copyProperties(stepInstance,settleWorkflowStepVo);
							settleWorkflowStepVo.setOperUser(users.get(settleWorkflowStepVo.getOperUser()));
							settleWorkflowStepVos.add(settleWorkflowStepVo);
						}
					}
					qzWorkflowVo.setStepInstanceVoList(settleWorkflowStepVos);
					a.add(qzWorkflowVo);
				});
			}
		}else if ("1".equals(qzWorkflowInstanceReq.getType())){
			SettleWorkflowInstance settleWorkflowInstance = settleWorkflowInstanceDao.getOne(new LambdaQueryWrapper<SettleWorkflowInstance>().eq(SettleWorkflowInstance::getInstCode, qzWorkflowInstanceReq.getInstCode()));
			QZWorkflowVo qzWorkflowVo = new QZWorkflowVo();
			qzWorkflowVo.setInstCode(settleWorkflowInstance.getInstCode());
			qzWorkflowVo.setFlowCode(settleWorkflowInstance.getFlowCode());
			qzWorkflowVo.setFlowName(settleWorkflowInstance.getFlowName());
			List<SettleWorkflowStepInstVo> settleWorkflowStepVos = new ArrayList<>();
			List<SettleWorkflowStepInstance> settleWorkflowStepInstance = settleWorkflowStepInstanceDao.list(new LambdaQueryWrapper<SettleWorkflowStepInstance>().eq(SettleWorkflowStepInstance::getInstCode, qzWorkflowInstanceReq.getInstCode()).eq(SettleWorkflowStepInstance::getStepInstState, '2').orderByAsc(SettleWorkflowStepInstance::getCreateDate));
			if (CollectionUtil.isNotEmpty(settleWorkflowStepInstance)){
				List<String> userId = settleWorkflowStepInstance.stream().map(SettleWorkflowStepInstance::getOperUser).distinct().collect(Collectors.toList());
				List<AuthUser> authUsers = authUserDao.listByIds(userId);
				Map<String,String> users = authUsers.stream().collect(Collectors.toMap(user -> { return user.getId() + ""; }, AuthUser::getName));
				for (SettleWorkflowStepInstance stepInstance : settleWorkflowStepInstance) {
					SettleWorkflowStepInstVo settleWorkflowStepVo = new SettleWorkflowStepInstVo();
					BeanUtil.copyProperties(stepInstance,settleWorkflowStepVo);
					settleWorkflowStepVo.setOperUser(users.get(settleWorkflowStepVo.getOperUser()));
					settleWorkflowStepVos.add(settleWorkflowStepVo);
				}
			}
			qzWorkflowVo.setStepInstanceVoList(settleWorkflowStepVos);
			a.add(qzWorkflowVo);
		}
		return R.success(a);
	}


	@ApiOperation("查询主流程表单")
	@PostMapping("/flow/settleWorkflowInstance/getMainPdfList")
	public  R<List<SettleBizformPdfVo>> getMainPdfList(@RequestBody @Valid WorkflowStepDataReq req){
		List<SettleBizformPdfVo>  list=settleWorkflowInstanceService.getMainPdfList(req);
     return  R.success(list);
	}

	@ApiOperation("获取前置流程表单列表")
	@PostMapping("/flow/settleWorkflowInstance/getFrontFlowList")
	public R<List<SettleBizformPdfVo>> getFrontFlowList(@RequestBody @Valid WorkflowStepDataReq req) {
		List<SettleBizformPdfVo> list = settleWorkflowInstanceService.getFrontFlowList(req);
		return R.success(list);
	}

	@PostMapping("/flow/getProcess/list")
	@ApiOperation("获取前置流程列表")
	public R getProcessList(@RequestBody processListReq jsonObject) {
		return settleWorkflowInstanceService.getProcessList(jsonObject);
	}
}
